package com.chenwc.wso2.wso2esb;

import com.chenwc.file.Decompression;
import com.chenwc.httpclient.RestMock;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WSO2 ESB 5.0.0 和 WSO2 EI 6.0.0 测试正常
 * @author chenwc
 * @date 2023/8/31 22:14
 */
public class TestWso2EsbMain {

    private final static Logger log = LoggerFactory.getLogger(TestWso2EsbMain.class);

    public static void main(String[] args) {

        //清除本地cookie
        RestMock.cookieStore.clear();

        Wso2Properties wso2Properties = new Wso2Properties();
        //登录的用户名密码
        wso2Properties.setPassword("admin");
        wso2Properties.setUsername("admin");
        wso2Properties.setWso2Host("https://localhost:9443");
        wso2Properties.setSaveFilePath("E:\\IdeaProjects_Workspace\\wso2-upload-car-file\\saveFile\\");

        //设置本地cookie
        setLocalCookie(RestMock.getHost(wso2Properties.getWso2Host()));

        List<Map<String, String>> carbonAppList;
        //获取碳应用列表
        carbonAppList = Wso2CarFileUtil.getCarbonAppList(wso2Properties);
        log.info("碳应用列表：{}", carbonAppList);

        //上传car包
        File carFile = new File("D:\\Downloads\\USERS.RESPONSE_MSGCompositeExporter_1.0.0-SNAPSHOT.car");

        //根据 car 包内的 artifacts.xml 文件读取到的 appName 和 appVersion
        Map<String, String> carMap = Decompression.getUploadCarbonAppName(carFile);
        String uploadCarbonAppName = carMap.get("appName");
        String uploadCarbonAppVersion = carMap.get("appVersion");

        //上传car包
        Wso2CarFileUtil.uploadCarFile(wso2Properties, carFile);
        try {
            //上传成功之后要等到一段时间才可以下载
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //下载工件
        Wso2CarFileUtil.downloadCarFile(wso2Properties, carMap);

        Map<String, String> deleteCarMap = new HashMap<>();
        deleteCarMap.put("appName", uploadCarbonAppName);
        deleteCarMap.put("appVersion", uploadCarbonAppVersion);
        //删除工件
        Wso2CarFileUtil.deleteCarFile(wso2Properties, deleteCarMap, false);

    }

    /**
     * 设置本地Cookie
     *
     * @param domain 设置范围
     */
    private static void setLocalCookie(String domain) {
        BasicClientCookie region1_configure_menu = new BasicClientCookie("region1_configure_menu", "none");
        region1_configure_menu.setDomain(domain);
        region1_configure_menu.setSecure(true);
        region1_configure_menu.setPath("/");

        BasicClientCookie region3_registry_menu = new BasicClientCookie("region3_registry_menu", "none");
        region3_registry_menu.setDomain(domain);
        region3_registry_menu.setSecure(true);
        region3_registry_menu.setPath("/");

        BasicClientCookie region4_monitor_menu = new BasicClientCookie("region4_monitor_menu", "none");
        region4_monitor_menu.setDomain(domain);
        region4_monitor_menu.setSecure(true);
        region4_monitor_menu.setPath("/");

        BasicClientCookie region5_tools_menu = new BasicClientCookie("region5_tools_menu", "none");
        region5_tools_menu.setDomain(domain);
        region5_tools_menu.setSecure(true);
        region5_tools_menu.setPath("/");

        BasicClientCookie menuPanel = new BasicClientCookie("menuPanel", "visible");
        menuPanel.setDomain(domain);
        menuPanel.setPath("/");

        BasicClientCookie menuPanelType = new BasicClientCookie("menuPanelType", "main");
        menuPanelType.setDomain(domain);
        menuPanelType.setPath("/");

        RestMock.cookieStore.addCookie(region1_configure_menu);
        RestMock.cookieStore.addCookie(region3_registry_menu);
        RestMock.cookieStore.addCookie(region4_monitor_menu);
        RestMock.cookieStore.addCookie(region5_tools_menu);
        RestMock.cookieStore.addCookie(menuPanel);
        RestMock.cookieStore.addCookie(menuPanelType);
    }
}
